package edu.ege.thesis.query.nyt.dao;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import edu.ege.thesis.model.NytDO;
import edu.ege.thesis.nyt.TimesArticleSearch;
import edu.ege.thesis.nyt.TimesArticleSearchResult;
import edu.ege.thesis.nyt.TimesResultObject;
import edu.ege.thesis.query.Endpoints;
import edu.ege.thesis.query.QueryStrings;
import edu.ege.thesis.query.dao.SPARQLQueryDAO;

public class NYTResourceDAO {
	Logger logger = LoggerFactory.getLogger(NYTResourceDAO.class);
	QueryStrings queryStrings;
	
	public NYTResourceDAO() {
		super();
		queryStrings = new QueryStrings();
	}

	public String getDbpediaResultforNYT(String nytResourceURI){
		
		String requiredURI = null;
		String nytIdQueryString = String.format(queryStrings.getNytIdQueryString(), nytResourceURI);
		
		SPARQLQueryDAO sparqlQueryOperations = new SPARQLQueryDAO(Endpoints.dbpediaEndpoint,nytIdQueryString);
		boolean flag=sparqlQueryOperations.initialize();
		if(flag==true){
			requiredURI = sparqlQueryOperations.getElement("?nytId").toString();
			logger.info("Required URI: " + requiredURI);
		}
		sparqlQueryOperations.closeQueryConnection();
		return requiredURI;
	}
	
	public String getNYTQuery(String nytResourceURI){
		String query = null;
		String nytQueryQueryString = String.format(queryStrings.getNytQueryQueryString(), getDbpediaResultforNYT(nytResourceURI));
		
		SPARQLQueryDAO sparqlQueryOperations = new SPARQLQueryDAO(Endpoints.sesameNYTEndpoint,nytQueryQueryString);
		boolean flag=sparqlQueryOperations.initialize();
		if(flag==true){
			query = sparqlQueryOperations.getElement("?query").toString();
			logger.info("Query: " + query);
		}
		sparqlQueryOperations.closeQueryConnection();
		return query;
	}
	
	public NytDO parseNYTQuery(String query){
		int a = query.indexOf("query=+");
		int b = query.indexOf("&rank");
	    String subStr = query.substring(a+(new String("query=+").length()), b);
	    logger.info("Substring: "+subStr);
	    
	    int c=subStr.indexOf("%3A%5B");
	    int d=subStr.indexOf("%5D");
	    
	    String facet = subStr.substring(0, c);
	    String concept = subStr.substring(c + (new String("%3A%5B").length()), d);
	    logger.info("Facet: " + facet +  " Concept: " + concept);
	    
	    return new NytDO(facet,concept); 
	    
	}
	
	public void searchTimesArticles(NytDO nytBean){
		TimesArticleSearch mySearch = new TimesArticleSearch();
	      
	    mySearch.addFacetQuery(nytBean.getFacet(), nytBean.getConcept()); //ask for articles from this desk

	    TimesArticleSearchResult r = mySearch.doSearch();
	    TimesResultObject[] tro = r.getResults();
	    for (int i = 0; i < tro.length; i++) {
	    	//   System.out.println("AUTHOR #" + i + ": " + tro[i].getAuthor());
	    	logger.info("TITLE #" + i + ": " + tro[i].getTitle());
	    	logger.info("ABSTRACT #" + i + ": " + tro[i].get_abstract());
	    	logger.info("BODY #" + i + ": " + tro[i].getBody());
	     };
	}
}
